package com.wswy.wzcx.widget.home

import android.content.Context
import android.content.Intent
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import com.blankj.utilcode.util.SizeUtils
import com.wswy.wzcx.R
import com.wswy.wzcx.model.license.UserCarInfo
import com.wswy.wzcx.ui.car.AddCarPreActivity
import com.wswy.wzcx.widget.home.IHomeCarLayout.Companion.SIZE
import kotlinx.android.synthetic.main.item_layout_home_add_car2.view.*
import kotlin.math.abs

class AddCarLayout : FrameLayout ,IHomeCarLayout{

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attribute: AttributeSet?) : this(context, attribute, 0)

    constructor(context: Context, attribute: AttributeSet?, defStyle: Int) : super(context, attribute, defStyle) {


    }

    init {
        LayoutInflater.from(context).inflate(R.layout.item_layout_home_add_car2, this)
        findViewById<View>(R.id.layout_bottom_car_info).apply {

            setOnClickListener {

                context.startActivity(Intent(context, AddCarPreActivity::class.java))
            }
        }

    }


    override fun alpha(alpha: Float) {
        this.alpha = alpha
    }



    override fun setData(userCarInfo: UserCarInfo?) {
    }

    private val dp40 by lazy {
        SizeUtils.dp2px(40f).toFloat()
    }


    private val dp30 by lazy {
        SizeUtils.dp2px(30f).toFloat()
    }

    override fun hideLeft() {
        ll_car_info.y = 300f
        img_car_icon.x = SIZE.screenWidget/2

        ll_car_info.alpha = 0f
        img_car_icon.alpha = 0f

        x = -SIZE.screenWidget
    }

    override fun hideRight() {
        ll_car_info.y = -300f
        img_car_icon.x = - SIZE.screenWidget
        ll_car_info.alpha = 0f
        img_car_icon.alpha = 0f

        x = SIZE.screenWidget

    }

    private var maxS = 0

    override fun animOffset(offset: Float) {
        x=0f


        if (img_car_icon.width == 0){

            postOnAnimation {
                requestLayout()
                maxS ++

                if (maxS < 30){
                    animOffset(offset)
                }
            }

            return
        }else{
            maxS = 0
        }


        ll_car_info.y = SizeUtils.dp2px(18f) + dp30 * (offset*-1)
        ll_car_info.alpha = 1 - abs(offset)


        val sw = SIZE.screenWidget - SizeUtils.dp2px(9f) //减去间距
        val wf = sw - SIZE.screenWidget * (1 - 0.8f) - img_car_icon.width / 2
        img_car_icon.x = wf + (sw - wf) * offset

        img_car_icon.alpha = 1- abs(offset)

        layout_bottom_car_info.alpha = 1- abs(offset)

        if (offset > 0){
            layout_bottom_car_info.y = SizeUtils.dp2px(126f) - dp40 * (1-offset)
        }else if (offset <= 0){
            layout_bottom_car_info.y = SizeUtils.dp2px(86f) + dp40 * abs(offset)
        }

        layout_bottom_car_info.layoutParams.run {

//            height = (dp40 -  ((20f) - 20f  *(1- abs(offset)) )).toInt()
//
//            layout_bottom_car_info.layoutParams = this
        }

    }

    override fun getView(): View = this


}